#!/bin/bash

if [ "$#" != "1" ]
then
	echo "Usage: run_tests.bash <hostname>"
	exit -1
fi

hostname=$1

#********************************************************************************************************************

# TESTING /json/item BEGINS HERE

# POST /json/item/

method="POST"
target="json/item/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_item_post.in"
curl -i -H "Accept: application/json" -X POST -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
	
	# GET /json/item/

	method="GET"
	target="json/item/"
	expectedOutput="./test_data/json_item_after_post.out"
	curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
	if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
	then
		echo "GET for POST AT $target *****FAILED*****"
		echo "Expected:"
		cat $expectedOutput
		echo "Recived:"
		cat tmpTestOutput
	else
		echo "GET for POST AT $target PASSED"

	fi

fi

# TESTING /json/item/1/ BEGINS HERE

# GET /json/item/1/

method="GET"
target="json/item/1/"
expectedOutput="./test_data/json_item_1_get.out"
curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
then
	echo "GET for POST AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutput
else
	echo "GET for POST AT $target PASSED"
fi

# PUT /json/item/1/

method="PUT"
target="json/item/1/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_item_1_put.in"
curl -i -H "Accept: application/json" -X $method -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
	
	# GET /json/item/1/

	method="GET"
	target="json/item/1/"
	expectedOutput="./test_data/json_item_1_after_put.out"
	curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
	if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
	then
		echo "GET for PUT AT $target *****FAILED*****"
		echo "Expected:"
		cat $expectedOutput
		echo "Recived:"
		cat tmpTestOutput
	else
		echo "GET for PUT AT $target PASSED"

	fi

fi

#********************************************************************************************************************

# TESTING /json/category BEGINS HERE

# POST /json/category/

method="POST"
target="json/category/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_category_post.in"
curl -i -H "Accept: application/json" -X POST -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
	
	# GET /json/category/

	method="GET"
	target="json/category/"
	expectedOutput="./test_data/json_category_after_post.out"
	curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
	if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
	then
		echo "GET for POST AT $target *****FAILED*****"
		echo "Expected:"
		cat $expectedOutput
		echo "Recived:"
		cat tmpTestOutput
	else
		echo "GET for POST AT $target PASSED"

	fi

fi

# TESTING /json/category/1/ BEGINS HERE

# GET /json/category/1/

method="GET"
target="json/category/1/"
expectedOutput="./test_data/json_category_1_get.out"
curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
then
	echo "GET for POST AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutput
else
	echo "GET for POST AT $target PASSED"
fi

# PUT /json/category/1/

method="PUT"
target="json/category/1/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_category_1_put.in"
curl -i -H "Accept: application/json" -X $method -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
	
	# GET /json/category/1/

	method="GET"
	target="json/category/1/"
	expectedOutput="./test_data/json_category_1_after_put.out"
	curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
	if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
	then
		echo "GET for PUT AT $target *****FAILED*****"
		echo "Expected:"
		cat $expectedOutput
		echo "Recived:"
		cat tmpTestOutput
	else
		echo "GET for PUT AT $target PASSED"

	fi

fi

#********************************************************************************************************************

# TESTING /json/recipie BEGINS HERE

# POST /json/recipie/

method="POST"
target="json/recipie/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_recipie_post.in"
curl -i -H "Accept: application/json" -X POST -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
	
	# GET /json/recipie/

	method="GET"
	target="json/recipie/"
	expectedOutput="./test_data/json_recipie_after_post.out"
	curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
	if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
	then
		echo "GET for POST AT $target *****FAILED*****"
		echo "Expected:"
		cat $expectedOutput
		echo "Recived:"
		cat tmpTestOutput
	else
		echo "GET for POST AT $target PASSED"

	fi

fi

# TESTING /json/recipie/1/ BEGINS HERE

# GET /json/recipie/1/

method="GET"
target="json/recipie/1/"
expectedOutput="./test_data/json_recipie_1_get.out"
curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
then
	echo "GET for POST AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutput
else
	echo "GET for POST AT $target PASSED"
fi

# PUT /json/recipie/1/

method="PUT"
target="json/recipie/1/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_recipie_1_put.in"
curl -i -H "Accept: application/json" -X $method -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
	
	# GET /json/recipie/1/

	method="GET"
	target="json/recipie/1/"
	expectedOutput="./test_data/json_recipie_1_after_put.out"
	curl -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
	if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
	then
		echo "GET for PUT AT $target *****FAILED*****"
		echo "Expected:"
		cat $expectedOutput
		echo "Recived:"
		cat tmpTestOutput
	else
		echo "GET for PUT AT $target PASSED"

	fi

fi

#********************************************************************************************************************

# TESTING /json/recipieSearch BEGINS HERE

# POST /json/recipieSearch/

method="POST"
target="json/recipieSearch/"
expectedOutput="./test_data/json_recipie_search.out"
inputFile="./test_data/json_recipie_search_post.in"
curl -H "Accept: application/json" -X $method -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutput
else
	echo "$method AT $target PASSED"
fi

#********************************************************************************************************************

# TESTING /json/registerUser BEGINS HERE

# POST /json/registerUser/

method="POST"
target="json/registerUser/"
expectedOutput="./test_data/passed_200.out"
inputFile="./test_data/json_register_user_post.in"
curl -i -H "Accept: application/json" -X $method -d "`cat $inputFile`" $hostname""$target > tmpTestOutput 2>/dev/null
head -n 1 tmpTestOutput > tmpTestOutputFirstLine
if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutputFirstLine >/dev/null 2>/dev/null
then
	echo "$method AT $target *****FAILED*****"
	echo "Expected:"
	cat $expectedOutput
	echo "Recived:"
	cat tmpTestOutputFirstLine
else
	echo "$method AT $target PASSED"
fi



# DELETE EVERYTHING BEGINS HERE
# it's the apocalyppppppppseeeeeeeeeee

# DELETE /json/item/1/

#method="DELETE"
#target="json/item/1/"
#expectedOutput="./test_data/passed_200.out"
#curl -i -X $method -H "Accept: application/json" $hostname""$target > tmpTestOutput 2>/dev/null
#if ! diff --ignore-tab-expansion --ignore-all-space --ignore-blank-lines --strip-trailing-cr --brief $expectedOutput tmpTestOutput >/dev/null 2>/dev/null
#then
#	echo "DELETE AT $target *****FAILED*****"
#else
#	echo "DELETE AT $target PASSED"
#fi
